package com.longxi.tianjintianbao.ui.presenter.rectification;

import android.content.Intent;
import android.text.TextUtils;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.rectification.RectificationItem;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.rectification.IRectificationAtView;
import com.longxi.tianjintianbao.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RectificationAtPresenter extends BasePresenter<IRectificationAtView> {

    public static String RECTIFICATIONITEM = "RECTIFICATIONITEM";

    RectificationItem rectificationItem;

    public String remark = "已完成";

    public RectificationAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        rectificationItem = (RectificationItem) intent.getSerializableExtra(RECTIFICATIONITEM);
        mContext.setTitle("整改完成");
        mContext.setTvLeft(MyUtils.getString(R.string.back));
        getView().getEtMeasures().setText(rectificationItem.getContent());
        getView().getEtReason().setText(rectificationItem.getReason());
        getView().getEtContents().setText(rectificationItem.getExplain());
        //添加字段
    }

    /**
     * 品质整改保存
     */
    public void saveRectification() {
        String measures = getView().getEtMeasures().getText().toString().trim();
        String reason = getView().getEtReason().getText().toString().trim();
        String contents = getView().getEtContents().getText().toString().trim();
        if(TextUtils.isEmpty(measures)){
            MyUtils.showToast(getView().getEtContents().getHint().toString());
            return;
        }

        if(TextUtils.isEmpty(reason)){
            MyUtils.showToast(getView().getEtReason().getHint().toString());
            return;
        }

        if(TextUtils.isEmpty(contents)){
            MyUtils.showToast(getView().getEtContents().getHint().toString());
            return;
        }

        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("itemid", rectificationItem.getItemid());
            object.put("measures", measures);
            object.put("reason", reason);
            object.put("contents", remark);
            object.put("remark", contents);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().saveRectification(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                if(baseResponse.getCode() != 0){
                    MyUtils.showToast(baseResponse.getMsg());
                    return;
                }
                MyUtils.showToast("保存成功");
                mContext.setResult(mContext.RESULT_OK);
                mContext.finish();
            }
        });
    }
}
